package com.innovations.inn.service.impl;

import com.innovations.common.core.domain.entity.SysUser;
import com.innovations.common.exception.ServiceException;
import com.innovations.common.utils.DateUtils;
import com.innovations.common.utils.StringUtils;
import com.innovations.common.utils.uuid.IdGeneratorSnowflake;
import com.innovations.framework.web.service.SysPermissionService;
import com.innovations.inn.domain.BusinessIndividual;
import com.innovations.inn.domain.Enterprise;
import com.innovations.inn.domain.OutcomesClassifyRelate;
import com.innovations.inn.domain.OutcomesTagRelate;
import com.innovations.inn.domain.ResearchInstitution;
import com.innovations.inn.domain.ResearchOutcomes;
import com.innovations.inn.domain.ResearchOutcomesClassify;
import com.innovations.inn.domain.TagInfo;
import com.innovations.inn.domain.request.RequestFilterListPreview;
import com.innovations.inn.domain.request.RequestListPreview;
import com.innovations.inn.domain.request.RequestResearchImport;
import com.innovations.inn.domain.request.RequestResearchOutcomes;
import com.innovations.inn.domain.request.RequestUnitInfo;
import com.innovations.inn.domain.response.ResponseItemCount;
import com.innovations.inn.domain.response.ResponseListPreview;
import com.innovations.inn.domain.response.ResponseResearchOutcomes;
import com.innovations.inn.domain.response.ResponseResearchOutcomesList;
import com.innovations.inn.domain.response.ResponseUnitInfo;
import com.innovations.inn.mapper.ResearchOutcomesMapper;
import com.innovations.inn.mapper.UnitInfoMapper;
import com.innovations.inn.service.IBusinessIndividualService;
import com.innovations.inn.service.IEnterpriseService;
import com.innovations.inn.service.IOutcomesClassifyRelateService;
import com.innovations.inn.service.IOutcomesTagRelateService;
import com.innovations.inn.service.IResearchInstitutionService;
import com.innovations.inn.service.IResearchOutcomesClassifyService;
import com.innovations.inn.service.IResearchOutcomesService;
import com.innovations.inn.service.ITagInfoService;
import com.innovations.inn.service.IValueSetService;
import com.innovations.system.service.ISysRoleService;
import com.innovations.system.service.ISysUserService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static com.innovations.common.utils.MyBeanUtils.copyProperties;

/**
 * 科研成果Service业务层处理
 *
 * @author dp
 * @date 2024-08-10
 */
@Slf4j
@Service
public class ResearchOutcomesServiceImpl implements IResearchOutcomesService {
    @Autowired
    ResearchOutcomesMapper researchOutcomesMapper;
    @Autowired
    IOutcomesTagRelateService outcomesTagRelateService;
    @Autowired
    IOutcomesClassifyRelateService outcomesClassifyRelateService;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;
    @Autowired
    ITagInfoService tagInfoService;
    @Autowired
    IResearchInstitutionService researchInstitutionService;
    @Autowired
    IResearchOutcomesClassifyService researchOutcomesClassifyService;
    @Autowired
    IValueSetService valueSetService;
    @Autowired
    IBusinessIndividualService businessIndividualService;
    @Autowired
    UnitInfoMapper unitInfoMapper;
    @Autowired
    IEnterpriseService enterpriseService;
    @Autowired
    ISysRoleService sysRoleService;
    @Autowired
    SysPermissionService sysPermissionService;
    @Autowired
    ISysUserService sysUserService;


    /**
     * 查询科研成果
     *
     * @param researchOutcomesId 科研成果主键
     * @return 科研成果
     */
    @Override
    public ResponseResearchOutcomes selectResearchOutcomesByResearchOutcomesId(Long researchOutcomesId) {
        ResponseResearchOutcomes response = new ResponseResearchOutcomes();
        // 获取科研成果主数据
        ResearchOutcomes researchOutcomes = researchOutcomesMapper.selectResearchOutcomesByResearchOutcomesId(researchOutcomesId);
        response = copyProperties(researchOutcomes, ResponseResearchOutcomes.class);

        // 获取单位信息
        RequestUnitInfo requestUnitInfo = new RequestUnitInfo();
        requestUnitInfo.setResearchOutcomesId(researchOutcomesId);
        requestUnitInfo.setUserType(researchOutcomes.getUserType());
        ResponseUnitInfo unitInfo = unitInfoMapper.selectUnitInfo(requestUnitInfo);
        if (unitInfo != null) {
            response.setUnitName(unitInfo.getUnitName());
            response.setUnitArea(unitInfo.getUnitArea());
            response.setUnitContact(unitInfo.getUnitContact());
            response.setUnitPhone(unitInfo.getUnitPhone());
        }

        // 获取科研成果分类明细
        List<Long> classifyIdList = new ArrayList<>();
        List<OutcomesClassifyRelate> classifyDetail = outcomesClassifyRelateService.selectOutcomesClassifyRelateByOutcomesId(researchOutcomesId);
        classifyDetail.forEach(item -> {
            classifyIdList.add(item.getClassifyId());
        });
        response.setClassifyIdList(classifyIdList);
        // 获取科研成果标签明细
        List<String> tagDescList = new ArrayList<>();
        List<OutcomesTagRelate> tagDetail = outcomesTagRelateService.selectOutcomesTagRelateByOutcomesId(researchOutcomesId);
        tagDetail.forEach(item -> {
            tagDescList.add(item.getTagDesc());
        });
        response.setTagDescList(tagDescList);

        return response;
    }

    /**
     * 查询科研成果列表
     *
     * @param researchOutcomes 科研成果
     * @return 科研成果
     */
    @Override
    public List<ResponseResearchOutcomesList> selectResearchOutcomesList(ResearchOutcomes researchOutcomes) {
        return researchOutcomesMapper.selectResearchOutcomesList(researchOutcomes);
    }

    /**
     * 查询科研成果合计
     *
     * @param researchOutcomes 科研成果
     * @return 科研成果合计
     */
    @Override
    public List<ResponseItemCount> selectResearchOutcomesCount(ResearchOutcomes researchOutcomes) {
        return researchOutcomesMapper.selectResearchOutcomesCount(researchOutcomes);
    }

    /**
     * 查询科研成果列表-预览展示
     *
     * @param requestListPreview 科研成果列表预览入参
     * @return 科研成果集合
     */
    public List<ResponseListPreview> selectOutcomesPreview(RequestListPreview requestListPreview) {
        List<ResponseListPreview> researchOutcomes = researchOutcomesMapper.selectOutcomesPreview(requestListPreview);
//        switch (requestListPreview.getQueryType()) {
//            //推荐
//            case 1:
//                researchOutcomes = researchOutcomesMapper.selectOutcomesPreview(requestListPreview);
//                break;
//            //关注
//            case 2:
//                researchOutcomes = researchOutcomesMapper.selectOutcomesPreviewAtt(requestListPreview);
//                break;
//            //全部
//            default:
//                researchOutcomes = researchOutcomesMapper.selectOutcomesPreview(requestListPreview);
//
//                break;
//    }
        return researchOutcomes;
    }

    /**
     * 查询科研成果列表-预览展示-过滤
     *
     * @param requestListPreview 科研成果列表预览入参
     * @return 科研成果集合
     */
    @Override
    public List<ResponseListPreview> selectOutcomesFilterPreview(RequestFilterListPreview requestListPreview) {
        List<ResponseListPreview> researchOutcomes = researchOutcomesMapper.selectOutcomesFilterPreview(requestListPreview);
        // 循环赋值单位信息
        researchOutcomes.forEach(item -> {
            // 获取单位信息
            RequestUnitInfo requestUnitInfo = new RequestUnitInfo();
            requestUnitInfo.setResearchOutcomesId(item.getResearchOutcomesId());
            requestUnitInfo.setUserType(item.getUserType());
            ResponseUnitInfo unitInfo = unitInfoMapper.selectUnitInfo(requestUnitInfo);
            if (unitInfo != null) {
                item.setUnitName(unitInfo.getUnitName());
                item.setUnitArea(unitInfo.getUnitArea());
                item.setUnitContact(unitInfo.getUnitContact());
                item.setUnitPhone(unitInfo.getUnitPhone());
            }
        });
        return researchOutcomes;
    }

    /**
     * 新增科研成果
     *
     * @param researchOutcomes 科研成果
     * @return 结果
     */
    @Override
    public Integer insertResearchOutcomes(RequestResearchOutcomes researchOutcomes) {
        try {
            // 验证提交内容是否有 base64 数据流
            if (researchOutcomes.getResearchOutcomesContent().contains(";base64,")) {
                throw new Exception("上传图片、视频、等文件请通过菜单栏按钮添加！");
            }

            // 保存科研成果主数据
            ResearchOutcomes response = copyProperties(researchOutcomes, ResearchOutcomes.class);
            researchOutcomesMapper.insertResearchOutcomes(response);

            //region 保存科研成果分类明细
            if (researchOutcomes.getClassifyIdList() != null) {
                List<OutcomesClassifyRelate> classifyRelateList = new ArrayList<>();
                // 循环添加分类明细
                for (Long classifyId : researchOutcomes.getClassifyIdList()) {
                    OutcomesClassifyRelate outcomesClassifyRelate = new OutcomesClassifyRelate();
                    outcomesClassifyRelate.setRelateId(idGeneratorSnowflake.nextId());
                    outcomesClassifyRelate.setResearchOutcomesId(response.getResearchOutcomesId());
                    outcomesClassifyRelate.setClassifyId(classifyId);
                    classifyRelateList.add(outcomesClassifyRelate);
                }
                outcomesClassifyRelateService.insertOutcomesClassifyRelateList(classifyRelateList);
            }
            //endregion 保存科研成果分类明细

            //region 保存科研成果标签明细
            List<OutcomesTagRelate> tagRelateList = new ArrayList<>();
            // 循环添加标签明细
            if (researchOutcomes.getTagDescList() != null) {
                for (String tagDesc : researchOutcomes.getTagDescList()) {
                    OutcomesTagRelate outcomesTagRelate = new OutcomesTagRelate();
                    // 判断标签字典是否存在
                    TagInfo tagInfo = new TagInfo();
                    tagInfoService.completeData(tagInfo, researchOutcomes.getCreateBy(), 1);
                    tagInfo.setTagDesc(tagDesc);
                    if (tagInfoService.selectTagInfoByTagDesc(tagInfo.getTagDesc()) == null) {
                        // 不存在则添加
                        tagInfoService.insertTagInfo(tagInfo);
                    } else {
                        // 存在则获取标签id
                        outcomesTagRelate.setTagId(tagInfo.getTagId());
                    }
                    outcomesTagRelate.setRelateId(idGeneratorSnowflake.nextId());
                    outcomesTagRelate.setResearchOutcomesId(response.getResearchOutcomesId());
                    outcomesTagRelate.setTagDesc(tagDesc);
                    // 添加成果对应标签明细列表
                    tagRelateList.add(outcomesTagRelate);
                }
            }
            outcomesTagRelateService.insertOutcomesTagRelate(tagRelateList);
            //endregion 保存科研成果标签明细
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    /**
     * 修改科研成果
     *
     * @param researchOutcomes 科研成果
     * @return 结果
     */
    @Override
    public Integer updateResearchOutcomes(RequestResearchOutcomes researchOutcomes) {
        try {
            // 1.保存科研成果主数据
            ResearchOutcomes response = copyProperties(researchOutcomes, ResearchOutcomes.class);
            researchOutcomesMapper.updateResearchOutcomes(response);

            //region 2.保存科研成果分类明细
            if (researchOutcomes.getClassifyIdList() != null) {
                // 2.1先删除
                outcomesClassifyRelateService.deleteOutcomesClassifyRelateByOutcomesId(response.getResearchOutcomesId());
                // 2.2后添加
                List<OutcomesClassifyRelate> classifyRelateList = new ArrayList<>();
                // 循环添加分类明细
                for (Long classifyId : researchOutcomes.getClassifyIdList()) {
                    OutcomesClassifyRelate outcomesClassifyRelate = new OutcomesClassifyRelate();
                    outcomesClassifyRelate.setRelateId(idGeneratorSnowflake.nextId());
                    outcomesClassifyRelate.setResearchOutcomesId(response.getResearchOutcomesId());
                    outcomesClassifyRelate.setClassifyId(classifyId);
                    classifyRelateList.add(outcomesClassifyRelate);
                }
                outcomesClassifyRelateService.insertOutcomesClassifyRelateList(classifyRelateList);
            }
            //endregion 保存科研成果分类明细

            //region 3.保存科研成果标签明细
            // 3.1先删除
            outcomesTagRelateService.deleteOutcomesTagRelateByResearchOutcomesId(response.getResearchOutcomesId());
            // 3.2后添加
            List<OutcomesTagRelate> tagRelateList = new ArrayList<>();
            // 循环添加标签明细
            for (String tagDesc : researchOutcomes.getTagDescList()) {
                OutcomesTagRelate outcomesTagRelate = new OutcomesTagRelate();
                // 3.3判断标签字典是否存在
                TagInfo tagInfo = new TagInfo();
                tagInfoService.completeData(tagInfo, researchOutcomes.getCreateBy(), 1);
                tagInfo.setTagDesc(tagDesc);
                if (tagInfoService.selectTagInfoByTagDesc(tagInfo.getTagDesc()) == null) {
                    // 不存在则添加
                    tagInfoService.insertTagInfo(tagInfo);
                } else {
                    // 存在则获取标签id
                    outcomesTagRelate.setTagId(tagInfo.getTagId());
                }

                outcomesTagRelate.setRelateId(idGeneratorSnowflake.nextId());
                outcomesTagRelate.setResearchOutcomesId(response.getResearchOutcomesId());
                outcomesTagRelate.setTagDesc(tagDesc);
                // 添加成果对应标签明细列表
                tagRelateList.add(outcomesTagRelate);
            }
            outcomesTagRelateService.insertOutcomesTagRelate(tagRelateList);
            //endregion 保存科研成果标签明细
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    /**
     * 批量删除科研成果
     *
     * @param researchOutcomesIds 需要删除的科研成果主键
     * @return 结果
     */
    @Override
    public Integer deleteResearchOutcomesByResearchOutcomesIds(Long[] researchOutcomesIds) {
        return researchOutcomesMapper.deleteResearchOutcomesByResearchOutcomesIds(researchOutcomesIds);
    }

    /**
     * 删除科研成果信息
     *
     * @param researchOutcomesId 科研成果主键
     * @return 结果
     */
    @Override
    public Integer deleteResearchOutcomesByResearchOutcomesId(Long researchOutcomesId) {
        return researchOutcomesMapper.deleteResearchOutcomesByResearchOutcomesId(researchOutcomesId);
    }

    /**
     * 导入科研成果
     *
     * @param researchList    导入科研成果数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param userId          操作用户
     * @return 结果
     */
    @Override
    public String importResearch(List<RequestResearchImport> researchList, Boolean isUpdateSupport, Long userId) {
        if (StringUtils.isNull(researchList) || researchList.size() == 0) {
            throw new ServiceException("导入科研成果数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RequestResearchImport researchImport : researchList) {
            try {
                // 1.转换数据
                RequestResearchOutcomes researchOutcomes = changeRequestResearchOutcomes(researchImport);
                if (researchOutcomes.getUserType() == null) {
                    //List<SysRole> sysRoleList = sysRoleService.selectRolesByUserId(userId);
                    SysUser user = sysUserService.selectUserById(userId);
                    Set<String> roles = sysPermissionService.getRolePermission(user);
                    for (String role : roles)
                        switch (role) {
                            // individual 个人 1
                            case "individual":
                                researchOutcomes.setUserType(1);
                                break;
                            // research 机构 2
                            case "research":
                                researchOutcomes.setUserType(2);
                                break;
                            // business 企业 3
                            case "business":
                                researchOutcomes.setUserType(3);
                                break;
                        }
                }
                if (researchOutcomes.getResearchOutcomesContent() == null) {
                    researchOutcomes.setResearchOutcomesContent("-");
                }
                if (researchImport.getResearchOutcomesTitle() == null) {
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、标题为空");
                    continue;
                }
                ResearchOutcomes researchCheck = new ResearchOutcomes();
                researchCheck.setResearchOutcomesTitle(researchOutcomes.getResearchOutcomesTitle());
                // 验证是否存在这个用户
                List<ResponseResearchOutcomesList> list = selectResearchOutcomesList(researchCheck);
                if (StringUtils.isNull(list) || list.isEmpty()) {
                    // 不存在，则添加
                    completeData(researchOutcomes, userId, 1);
                    Integer iReturn = insertResearchOutcomes(researchOutcomes);
                    if (iReturn == 0) {
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、成果 " + researchImport.getResearchOutcomesTitle() + " 导入失败");
                    }
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、成果 " + researchImport.getResearchOutcomesTitle() + " 导入成功");
                } else if (isUpdateSupport) {
                    // 存在且允许更新，则更新
                    completeData(researchOutcomes, userId, 2);
                    updateResearchOutcomes(researchOutcomes);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、成果 " + researchImport.getResearchOutcomesTitle() + " 更新成功");
                } else {
                    // 存在且不允许更新，则失败
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、成果 " + researchImport.getResearchOutcomesTitle() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、成果 " + researchImport.getResearchOutcomesTitle() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 转换导入科研成果数据
     *
     * @param researchImport
     * @return
     */
    private RequestResearchOutcomes changeRequestResearchOutcomes(RequestResearchImport researchImport) {
        // 清空标识，后续重新生成
        researchImport.setResearchOutcomesId(null);

        // 科研成果类别
        researchImport.setResearchOutcomesClass(
                valueSetService.selectValueBySystemNoAndDesc(
                        "research_outcomes_class",
                        researchImport.getResearchOutcomesClassName()));
        // 中医药成果类型
//        researchImport.setResearchOutcomesType(
//                valueSetService.selectValueBySystemNoAndDesc(
//                        "research_outcomes_type",
//                        researchImport.getResearchOutcomesTypeName()));
        List<Long> classifyIdList = new ArrayList<>();
        Arrays.asList(researchImport.getResearchOutcomesTypeName().split("\\|")).forEach(item -> {
            classifyIdList.add(valueSetService.selectValueBySystemNoAndDesc(
                    "research_outcomes_type",
                    researchImport.getResearchOutcomesTypeName()));
        });
        if (!classifyIdList.isEmpty()) {
            researchImport.setClassifyIdList(classifyIdList);
        }

        // 科研成果体现形式
        researchImport.setResearchOutcomesEmbodiment(
                valueSetService.selectValueBySystemNoAndDesc(
                        "research_outcomes_embodiment",
                        researchImport.getResearchOutcomesEmbodimentName()));
        // 科研成果阶段
        researchImport.setResearchOutcomesStage(
                valueSetService.selectValueBySystemNoAndDesc(
                        "research_outcomes_stage",
                        researchImport.getResearchOutcomesStageName()));
        // 科研成果应用状态
        researchImport.setResearchOutcomesApplicationState(
                valueSetService.selectValueBySystemNoAndDesc(
                        "research_outcomes_application_state",
                        researchImport.getResearchOutcomesApplicationStateName()));
        // 科研机构
        researchImport.setInstitutionId(
                researchInstitutionService.selectResearchInstitutionIdByName(
                        researchImport.getInstitutionName()));

        // 类型转化
        RequestResearchOutcomes researchOutcomes = copyProperties(researchImport, RequestResearchOutcomes.class);
        return researchOutcomes;
    }

    /**
     * 查询导出科研成果列表
     *
     * @param researchOutcomes 科研成果
     * @return 科研成果导出集合
     */
    @Override
    public List<RequestResearchImport> selectExport(ResearchOutcomes researchOutcomes) {
        List<RequestResearchImport> researchImportList = researchOutcomesMapper.selectExport(researchOutcomes);
        // 中医药成果类型
        researchImportList.forEach(item -> {
            //自增序号
            item.setResearchOutcomesId(null);
            OutcomesClassifyRelate outcomesClassifyRelate = new OutcomesClassifyRelate();
            outcomesClassifyRelate.setResearchOutcomesId(item.getResearchOutcomesId());
            List<OutcomesClassifyRelate> classifyIdList = outcomesClassifyRelateService.selectOutcomesClassifyRelateList(outcomesClassifyRelate);
            List<String> classifyListNames = new ArrayList<>();
            if (classifyIdList != null && !classifyIdList.isEmpty()) {
                classifyIdList.stream().forEach(item1 -> {
                    ResearchOutcomesClassify researchOutcomesClassify = researchOutcomesClassifyService
                            .selectResearchOutcomesClassifyByClassifyId(item1.getClassifyId());
                    if (researchOutcomesClassify != null) {
                        classifyListNames.add(researchOutcomesClassify.getClassifyDesc());
                    }
                });
            }
            item.setResearchOutcomesTypeName(StringUtils.join(classifyListNames, "|"));

//            // 科研成果类别
//            ValueSet valueSet1 = valueSetService.selectValueSetByValueId(item.getResearchOutcomesClass());
//            if (valueSet1 != null) {
//                item.setResearchOutcomesClassName(valueSet1.getValueDesc());
//            }
//            // 科研成果体现形式
//            ValueSet valueSet2 = valueSetService.selectValueSetByValueId(item.getResearchOutcomesEmbodiment());
//            if (valueSet1 != null) {
//                item.setResearchOutcomesEmbodimentName(valueSet2.getValueDesc());
//            }
//            // 科研成果阶段
//            ValueSet valueSet3 = valueSetService.selectValueSetByValueId(item.getResearchOutcomesStage());
//            if (valueSet1 != null) {
//                item.setResearchOutcomesStageName(valueSet3.getValueDesc());
//            }
//            // 科研成果应用状态
//            ValueSet valueSet4 = valueSetService.selectValueSetByValueId(item.getResearchOutcomesApplicationState());
//            if (valueSet1 != null) {
//                item.setResearchOutcomesApplicationStateName(valueSet4.getValueDesc());
//            }
//            // 科研机构
//            ResearchInstitution researchInstitution = researchInstitutionService
//                    .selectResearchInstitutionByInstitutionId(item.getInstitutionId());
//            if (researchInstitution != null) {
//                item.setInstitutionName(researchInstitution.getInstitutionName());
//            }

        });
        return researchImportList;
    }

    /**
     * 实体数据补全
     *
     * @param researchOutcomes 实体
     * @param userId           用户id
     * @param crateType        创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(RequestResearchOutcomes researchOutcomes, Long userId, Integer crateType) {
        // 主键
        if (researchOutcomes.getResearchOutcomesId() == null || "".equals(researchOutcomes.getResearchOutcomesDesc())) {
            researchOutcomes.setResearchOutcomesId(idGeneratorSnowflake.nextId());
        }
        // 创建模式
        if (crateType == 1) {
            // 判断用户类型
            if (researchOutcomes.getUserType() == null) {
                throw new ServiceException("用户类型不能为空！");
            }
            // 创建人、创建时间
            if (researchOutcomes.getCreateBy() == null) {
                researchOutcomes.setCreateBy(userId);
            }
            if (researchOutcomes.getCreateTime() == null) {
                researchOutcomes.setCreateTime(DateUtils.getNowDate());
            }
        }
        // 修改模式
        if (crateType == 2) {
            // 判断如果为【2】审核状态，且未审核过，则添加审核人、审核时间
            if (researchOutcomes.getState() == 2 && researchOutcomes.getSubmitBy() == null) {
                researchOutcomes.setSubmitBy(userId);
                researchOutcomes.setSubmitTime(DateUtils.getNowDate());
            }
            // 创建时间不为空时 设置为空
            if (researchOutcomes.getCreateTime() != null) {
                researchOutcomes.setCreateBy(null);
                researchOutcomes.setCreateTime(null);
            }
        }
        // 所属科研机构
        if (researchOutcomes.getInstitutionId() == null) {
            //用户类型(1.个人 2.机构 3.企业)及用户id获取机构id
            if (researchOutcomes.getUserType() == 1) {
                // 个人机构查询
                BusinessIndividual businessIndividualNew = new BusinessIndividual();
                businessIndividualNew.setUserId(userId);
                List<BusinessIndividual> list = businessIndividualService.selectBusinessIndividualList(businessIndividualNew);
                if (list == null || list.isEmpty()) {
                    throw new ServiceException("个人用户未绑定机构，请联系管理员！");
                } else {
                    researchOutcomes.setInstitutionId(list.get(0).getInstitutionId());
                }
            } else if (researchOutcomes.getUserType() == 2) {
                ResearchInstitution researchInstitution = new ResearchInstitution();
                researchInstitution.setUserId(userId);
                List<ResearchInstitution> list = researchInstitutionService.selectResearchInstitutionList(researchInstitution);
                if (list == null || list.isEmpty()) {
                    throw new ServiceException("机构用户未绑定机构，请联系管理员！");
                } else {
                    researchOutcomes.setInstitutionId(list.get(0).getInstitutionId());
                }
            } else if (researchOutcomes.getUserType() == 3) {
                Enterprise researchInstitution = new Enterprise();
                researchInstitution.setUserId(userId);
                List<Enterprise> list = enterpriseService.selectEnterpriseList(researchInstitution);
                if (list == null || list.isEmpty()) {
                    throw new ServiceException("企业用户未绑定机构，请联系管理员！");
                } else {
                    researchOutcomes.setInstitutionId(list.get(0).getEnterpriseId());
                }
            }
        }
    }

}
